package cn.rcth.base.serivce.impl;

import cn.rcth.base.VO.TemplateDataVo;
import cn.rcth.base.VO.WxMssVo;
import cn.rcth.base.domain.*;
import cn.rcth.base.mapper.FormIdMapper;
import cn.rcth.base.msg.AccessToken;
import cn.rcth.base.serivce.IDeliverySerivce;
import cn.rcth.base.serivce.IFormIdSerivce;
import cn.rcth.base.serivce.IOrderSerivce;
import cn.rcth.base.serivce.IUserSerivce;
import cn.rcth.base.util.Global;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class FormIdSerivceImple implements IFormIdSerivce {

    @Autowired
    private RestTemplate restTemplate;


    @Autowired
    private FormIdMapper formIdMapper;

    @Autowired
    private IOrderSerivce orderSerivce;

    @Autowired
    private IUserSerivce userSerivce;

    @Autowired
    private IDeliverySerivce deliverySerivce;

    @Override
    public void insertForm(FormId formid) {
        formIdMapper.insert(formid);
    }

    @Override
    public String pushOneUser(Long userId, String formid, Long orderId, String template)  {

        //获取access_token
        String access_token = getAccess_token(Global.appId, Global.secret);
        String url = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send" +
                "?access_token=" + access_token;

        //查询订单内容
        Order order = orderSerivce.selectOrderById(orderId);
        User user = userSerivce.getUser(userId);
        //拼接推送的模版
        WxMssVo wxMssVo = new WxMssVo();
        wxMssVo.setTouser(user.getOpenId());//用户openid
        wxMssVo.setTemplate_id(template);//模版id
        wxMssVo.setForm_id(formid);//formid

        Map<String, TemplateDataVo> m = new HashMap<>();

        List<OrderProduct> orderProducts = order.getOrderProducts();
        String ProductName = null;
        for (OrderProduct orderProduct : orderProducts) {
            ProductName = orderProduct.getName();
        }

        if (order.getStatus().equals(30)) {
            TemplateDataVo keyword1 = new TemplateDataVo();
            keyword1.setValue(ProductName);//商品名称
            m.put("keyword1", keyword1);
            wxMssVo.setData(m);
            System.out.println(keyword1.getValue() + ":name");

            Delivery delovery = deliverySerivce.getDelivery(orderId);
            TemplateDataVo keyword2 = new TemplateDataVo();
            keyword2.setValue(delovery.getDeliveryCompany() + "");//物流公司
            m.put("keyword2", keyword2);
            wxMssVo.setData(m);
            System.out.println(keyword2.getValue() + ":bianhao");

            TemplateDataVo keyword3 = new TemplateDataVo();
            keyword3.setValue(delovery.getDeliveryNumber() + "");//物流单号
            m.put("keyword3", keyword3);
            wxMssVo.setData(m);
            System.out.println(keyword3.getValue() + ":jine");

            TemplateDataVo keyword4 = new TemplateDataVo();
            keyword4.setValue(order.getDeliveryTime() + "");//发货时间
            m.put("keyword4", keyword4);
            wxMssVo.setData(m);
            System.out.println(keyword4.getValue() + ":time");
        } else if (order.getStatus().equals(40)) {

            TemplateDataVo keyword1 = new TemplateDataVo();
            keyword1.setValue(ProductName);//商品名称
            m.put("keyword1", keyword1);
            wxMssVo.setData(m);
            System.out.println(keyword1.getValue() + ":name");

            TemplateDataVo keyword2 = new TemplateDataVo();
            keyword2.setValue(order.getOrderSn() + "");//订单号
            m.put("keyword2", keyword2);
            wxMssVo.setData(m);
            System.out.println(keyword2.getValue() + ":bianhao");
        }

        ResponseEntity<String> responseEntity =
                restTemplate.postForEntity(url, wxMssVo, String.class);

        System.out.println(responseEntity.getBody() + "wancheng");

        return responseEntity.getBody();


    }


    /*
     * 获取access_token
     * appid和appsecret到小程序后台获取，当然也可以让小程序开发人员给你传过来
     * */
    public String getAccess_token(String appid, String appsecret) {
        //获取access_token
        String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential" +
                "&appid=" + appid + "&secret=" + appsecret;
        String json = restTemplate.getForObject(url, String.class);
        AccessToken accessToken = new Gson().fromJson(json, AccessToken.class);
        return accessToken.getAccess_token();
    }
}
